package com.pacific.sign.interceptor;

import com.pacific.sign.WebConstant;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by whz on 2014/12/23.
 */
public class UserLoginFilter implements HandlerInterceptor {
    private Logger logger = Logger.getLogger(getClass());

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String host = request.getRemoteAddr();
        logger.info("host: " + host);
        Integer userId = (Integer) request.getSession().getAttribute(WebConstant.CURRENT_LOGIN_USER_INFO);
        if (userId != null && userId > 0) {
            return true;
        } else {
            response.sendRedirect(request.getContextPath() + "/index");
            return false;
        }

    }

    private boolean isValid(String ip) {
//        List<String> ipList = uumSOAProxy.getIpConfigLiset();
//        if (StringUtils.isBlank(ip)) {
//            logger.error("ip is null");
//            return false;
//        }
//        if (ipList.contains(ip)) {
//            return true;
//        }
//        if (ip.lastIndexOf(".") != -1) {
//            ip = ip.substring(0, ip.lastIndexOf("."));
//            logger.debug("short ip: " + ip);
//        }
//        if (ipList.contains(ip)) {
//            return true;
//        } else {
//            logger.info("hims not valid ip: " + ip);
//            return false;
//        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
}
